/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectData;
import DTO.Category2;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Vien-IT
 */
public class BCategory2 {
    
    private ConnectData condata;

    public BCategory2() {
        condata = new ConnectData();
    }
    
    public ArrayList<Category2> getAll(String sql)
    {
        ArrayList<Category2> categories2 = new ArrayList<Category2>();
        Category2 category2 = null;
        try {
            ResultSet rs = condata.executeQuery(sql);
            while(rs.next())
            {
                category2 = new Category2();
                category2.setCategory2Id(rs.getInt("Category2Id"));
                category2.setName(rs.getString("name"));
                category2.setCategory1Id(rs.getInt("category1Id"));
                categories2.add(category2);
                condata.close();
            }
        } catch (Exception e) {
        }
        
        return categories2.isEmpty() ? null : categories2;
    }
    
    public ArrayList<Category2> getAll()
    {
        String sql = "select * from tblCategory2";
           return getAll(sql);
    }
    
    
    public Category2 getById(int category2)
    {
        String sql = "select * from tblCategory2 where Category2Id = "+category2+"";
        ArrayList<Category2> categories2 = getAll(sql);
        return categories2 == null ? null : categories2.get(0);
    }
    
    public ArrayList<Category2> getByCategory1Id(int category1Id)
    {
        String sql = "select * from tblCategory2 where category1Id = "+category1Id+"";
        return this.getAll(sql);
    }
    public boolean insert(Category2 category2)
    {
        boolean check = false;
        String sql = "insert into tblCategory2 (name) values (N'"+category2.getName()+"')";
        int rs = -1;
        try {
            rs = condata.executeUpdate(sql);
            if(rs > 0)
                check = true;
        } catch (Exception e) {
        }
        return check ;
    }
    
}
